package com.google.code.hibernate.rest.example;
import static com.google.code.hibernate.rest.internal.InternalPreconditions.checkNotNull;
import java.math.BigDecimal;

import org.hibernate.SessionFactory;
import org.hibernate.classic.Session;

/**
 * 
 * @author wangzijian
 * 
 */
public class EntityExample {

	public final Book englishBook;
	public final Book chineseBook;
	public final Student jackStudent;
	public final Teacher tomTeacher;
	public final StudentTeacher studentTeacher;

	public static EntityExample persistedFrom(SessionFactory sessionFactory) {
		checkNotNull(sessionFactory, "sessionFactory");
		return new EntityExample(sessionFactory);
	}
	
	private EntityExample(SessionFactory sessionFactory) {
		Session session = sessionFactory.openSession();

		englishBook = new Book();
		englishBook.setId(9);
		englishBook.setName("English");
		englishBook.setPrice(new BigDecimal("18.50"));
		session.save(englishBook);

		chineseBook = new Book();
		chineseBook.setId(10);
		chineseBook.setName("Chinese");
		chineseBook.setPrice(new BigDecimal("15.00"));
		session.save(chineseBook);

		jackStudent = new Student();
		jackStudent.setId(101);
		jackStudent.setName("Jack");
		Information information = new Information();
		information.setCode("ABC");
		information.setDescription("Hello World");
		jackStudent.setInformation(information);
		jackStudent.addBook(englishBook);
		jackStudent.addBook(chineseBook);
		session.save(jackStudent);

		tomTeacher = new Teacher();
		tomTeacher.setId(5);
		tomTeacher.setName("Tome");
		tomTeacher.setAge(50);
		session.save(tomTeacher);

		studentTeacher = new StudentTeacher(jackStudent, tomTeacher);
		studentTeacher.setId(60);
		session.save(studentTeacher);
		
		jackStudent.getStudentTeachers().add(studentTeacher);

		session.flush();
		session.close();
	}
	
}
